﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BLL;
using Model;
using System.Drawing.Printing;
using PrintDemo;
using System.Xml;
using System.IO;
using System.Configuration;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;
using System.IO.Compression;
using Newtonsoft.Json;

namespace Sales
{
    public partial class frmMain : Form
    {
        private static frmMain frmmain = null;

        private TempStorageH tshModel;
        private TempStorageB tsbModel;
        private TempStorageBLL tsBLL;

        string url = ConfigurationSettings.AppSettings["WebService"].ToString();


        private SpdaHBLL spdaHBLL;
        private HykblHBLL hykblhBLL;

        private XskdhBLL xskdhBLL;
        private POSPrintDocument posPrintDocument;
        private HykblHModel hykblhModel;
        private xskdB xskdBModel;
        private xskdh xskdhModel;
        private SpdaHModel spdahModel;

        private static DataTable spInfoData = null;
        private static DataTable oldspInfoData = null;
        private static string cdyGuid = "";

        public SysConfig config;//系统参数

        public Common2 common = new Common2();
        public static string config_dir = Application.StartupPath + "/config";
        public static string config_file = config_dir + "/config.xml";
        public static string wsconfig_file = config_dir + "/configWS.xml";
        public static string pla_file = config_dir + "/PlatformConfig.xml";
        #region 页面属性

        /// <summary>
        /// 制单人
        /// </summary>
        public string Zdr
        {
            get;
            set;

        }

        /// <summary>
        /// 店面编号
        /// </summary>
        public string Dmbh
        {
            set;
            get;
        }
        /// <summary>
        /// 店面名称
        /// </summary>
        public string Dmmc
        {
            set;
            get;
        }

        /// <summary>
        /// 仓库编号
        /// </summary>
        public string Ckbh
        {
            get;
            set;
        }
        /// <summary>
        /// 仓库名称
        /// </summary>
        public string Ckmc
        {
            get;
            set;
        }
        /// <summary>
        /// 营业员编号
        /// </summary>
        public string Yyybh
        {
            set;
            get;
        }
        /// <summary>
        /// 营业员名称
        /// </summary>
        public string Yyymc
        {
            set;
            get;
        }


        /// <summary>
        /// 收银员编号
        /// </summary>
        public string Syybh
        {
            set;
            get;
        }
        /// <summary>
        /// 收银员名称
        /// </summary>
        public string Syymc
        {
            set;
            get;
        }

        /// <summary>
        /// 光标位置
        /// </summary>
        public int CursorSite
        {
            set;
            get;
        }

        /// <summary>
        /// 会员编号(此处为业务方便 采用 卡号)
        /// </summary>
        public string Hybh
        {
            set;
            get;
        }
        /// <summary>
        /// 会员名称
        /// </summary>
        public string Hymc
        {
            set;
            get;
        }

        /// <summary>
        /// 会员类型
        /// </summary>
        public string Hylx
        {
            get;
            set;
        }
        /// <summary>
        /// 会员积分 余额
        /// </summary>
        public decimal Hyjf
        {
            get;
            set;
        }

        /// <summary>
        /// 会员积分 消费
        /// </summary>
        public string Hykxf
        {
            get;
            set;
        }



        /// <summary>
        /// 会员卡号
        /// </summary>
        public string Hykh
        {
            get;
            set;
        }

        /// <summary>
        /// 本次积分
        /// </summary>
        public string Bcjf
        {
            get;
            set;
        }

        /// <summary>
        /// 会员账号余额
        /// </summary>
        public decimal Zhye
        {
            get;
            set;
        }

        /// <summary>
        /// 最近消费积分
        /// </summary>
        public string Zjxfjf
        {
            get;
            set;
        }

        /// <summary>
        /// 会员折扣
        /// </summary>
        public string Hyzk
        {
            get;
            set;
        }

        /// <summary>
        /// 应收(未打折时候)
        /// </summary>
        public string Ys
        {
            set;
            get;
        }

        /// <summary>
        /// 实收
        /// </summary>
        public string Ss
        {
            set;
            get;
        }


        /// <summary>
        /// 实收金额
        /// </summary>
        public string SsJe
        {
            set;
            get;
        }

        /// <summary>
        /// 现金
        /// </summary>
        public string Xj
        {
            set;
            get;
        }

        /// <summary>
        /// 微信
        /// </summary>
        public string WX
        {
            set;
            get;
        }

        /// <summary>
        /// 银行卡
        /// </summary>
        public string YHK
        {
            set;
            get;
        }

        /// <summary>
        /// 支付宝
        /// </summary>
        public string ZFB
        {
            set;
            get;
        }




        /// <summary>
        /// 找零
        /// </summary>
        public string Zl
        {
            set;
            get;
        }

        /// <summary>
        /// 欠款
        /// </summary>
        public string Qk
        {
            set;
            get;
        }

        /// <summary>
        /// 整单折扣
        /// </summary>
        public decimal Zdzk
        {
            set;
            get;
        }

        /// <summary>
        /// 数量
        /// </summary>
        public string Sl
        {
            set;
            get;
        }

        /// <summary>
        /// 销售单号
        /// </summary>
        public string Xsdh
        {
            set;
            get;
        }

        /// <summary>
        /// 打印状态
        /// </summary>
        public int PrintState
        {
            set;
            get;
        }

        /// <summary>
        /// 扫描数量
        /// </summary>
        public int ScanNumber
        {
            set;
            get;
        }

        /// <summary>
        /// 退货状态
        /// </summary>
        public bool Th
        {
            set;
            get;
        }



        #endregion


        public frmMain()
        {
            InitializeComponent();
            MiddleModule.eventSend += new Send(MiddleModule_eventSend);
            this.txtSpbh.Font = new Font("宋体", 13, FontStyle.Bold);
            this.txtSpbh.Width = 300;

            config = Common2.GetConfig();//获取相关配置属性
            SetdataGridStyle();
            this.listSp.Font = new Font("宋体", 14, FontStyle.Bold);

            PrintState = 1;
            ScanNumber = 1;
            Zdzk = 1;

            Th = false;//默认为未退货状态

            MyTimer();
            this.Text = config.SysName;
        }
        private void MyTimer()
        {
            double spt = 1000;//每秒钟
            System.Timers.Timer MT = new System.Timers.Timer(spt);
            MT.Elapsed += new System.Timers.ElapsedEventHandler(MTimedEvent);
            MT.Enabled = true;
        }


        //构造System.Timers.Timer实例   间隔时间事件  
        private void MTimedEvent(object source, System.Timers.ElapsedEventArgs e)
        {
            int intHour = e.SignalTime.Hour;
            int intMinute = e.SignalTime.Minute;
            int intSecond = e.SignalTime.Second;

            //每十分钟定时执行
            if (intMinute % 10 == 0 && intSecond == 1)
            {
                UpLoadHykblHData();
                UpLoadHykczjlData();
            }

        }



        private void UpLoadHykblHData()
        {
            hykblhBLL = new HykblHBLL(url);
            DataSet ds = hykblhBLL.GetHykblHData();
            byte[] dtHykblh = GetDataSetSurrogateZipBYtes(ds);
            ArrayList parms = new ArrayList();
            parms.Add(dtHykblh);
            string reslut = DSWebService.InvokeWebService(url, "UpLoadHykbl", parms.ToArray());

            if (int.Parse(reslut) > 0)
            {
                List<string> sqlList = new List<string>();

                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    string ss = hykblhBLL.UpdateHykblH(dr["kh"].ToString());
                    sqlList.Add(ss);
                }
                if (sqlList.Count > 0)
                {
                    int ss = hykblhBLL.ExecuteSqlTran(sqlList);
                }

                LogHelper.m_CreateErrorLogTxt("UpLoadHykblHData", "0", "上传成功");
            }
            else
                LogHelper.m_CreateErrorLogTxt("UpLoadHykblHData", "0", "上传失败");

        }
        private void UpLoadHykczjlData()
        {
            hykblhBLL = new HykblHBLL(url);
            DataSet ds = hykblhBLL.GetHykczjlData();
            byte[] dtHykczjl = GetDataSetSurrogateZipBYtes(ds);
            ArrayList parms = new ArrayList();
            parms.Add(dtHykczjl);
            string reslut = DSWebService.InvokeWebService(url, "UpLoadHykczjl", parms.ToArray());

            if (int.Parse(reslut) > 0)
            {

                //  UpdateHykblH

                List<string> sqlList = new List<string>();

                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    string ss = hykblhBLL.UpdateHykczjl(dr["id"].ToString());
                    sqlList.Add(ss);
                }
                if (sqlList.Count > 0)
                {
                    int ss = hykblhBLL.ExecuteSqlTran(sqlList);
                }

                LogHelper.m_CreateErrorLogTxt("UpLoadHykczjlData", "0", "上传成功");
            }
            else
                LogHelper.m_CreateErrorLogTxt("UpLoadHykczjlData", "0", "上传失败");
        }

        private void DownloadGetYyyH()
        {
            ArrayList parms = new ArrayList();
            BasicBLL cpdaHBLL = new BasicBLL();
            string reslut = "";



            parms.Add(reslut);
            reslut = DSWebService.InvokeWebService(url, "GetYyyData", parms.ToArray());

            if (reslut != "-2")
            {

                if (reslut != "")
                {
                    DataTable dt = DeserializeDataTable(reslut);
                    //插入数据库
                    foreach (DataRow newdr in dt.Rows)
                    {
                        string ygbh = newdr["ygbh"].ToString();
                        yyydaHModel model = cpdaHBLL.GetyyydaHModel(newdr);
                        if (cpdaHBLL.IsExistYyy(ygbh))
                        {
                            bool s1 = cpdaHBLL.Updatejbxx_yyydaHModel(model);
                        }
                        else
                        {
                            bool s2 = cpdaHBLL.Addjbxx_yyydaHModel(model);
                        }
                    }

                    LogHelper.m_CreateErrorLogTxt("营业员信息  ", "0", " 下载完成！");
                }
                else
                {
                    LogHelper.m_CreateErrorLogTxt("营业员信息  ", "0", " 无数据下载！");
                }
            }
            else
            {
                // this.osProgress3.AutoProgress = false;
                // MessageBox.Show(" 无法连接目标服务器请检查网络！提取营业员信息失败 ");
            }
        }

        private void DownloadGetCpdaH()
        {
            ArrayList parms = new ArrayList();
            BasicBLL cpdaHBLL = new BasicBLL();
            string reslut = "";
            string strWhere = "";
            // string strWhere2 = "";

            string startTime = DateTime.Now.AddDays(-2).ToString("yyyy-MM-dd") + " 00:00:00";
            string endTime = DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59";

            strWhere = "(( xgsj>='" + startTime + "' and xgsj<='" + endTime + "') or ( djsj>='" + startTime + "' and djsj<='" + endTime + "'))";




            int count = 0;
            int pageIndex = 1;
            int pageSize = 50;
            while (true)
            {
                parms.Clear();
                parms.Add(strWhere);
                parms.Add(pageSize);
                parms.Add(pageIndex);

                reslut = DSWebService.InvokeWebService(url, "GetCpdaData", parms.ToArray());
                if (reslut != "-2" && reslut != "")
                {
                    pageIndex++;
                    DataTable dt = DeserializeDataTable(reslut);
                    count += dt.Rows.Count;
                    //插入数据库
                    foreach (DataRow newdr in dt.Rows)
                    {
                        string spbh = newdr["spbh"].ToString();
                        SpdaHModel model = cpdaHBLL.GetspdaHModel(newdr);
                        if (cpdaHBLL.IsExistSpbh(spbh))
                        {
                            bool s1 = cpdaHBLL.UpdatespdaHModel(model);
                        }
                        else
                        {
                            bool s2 = cpdaHBLL.AddspdaHModel(model);
                        }
                    }
                }
                else
                {
                    break;
                }
            }
            if (count > 0)
            {
                //this.osProgress2.AutoProgress = false;

                //MessageBox.Show(" 下载完成！ 共计下载数据！ ");
                //MessageBox.Show(" 下载完成！ 共计下载数据" + count + "条");
            }
            else
            {

                //this.osProgress2.AutoProgress = false;
                //MessageBox.Show(" 本时间段未获取到数据，可能网络原因，可能本时间段无更新数据！ ");
            }



        }

        private void UpLoadData()
        {
            BasicBLL basicBLL = new BasicBLL();
            // int Count = basicBLL.GetxskdHCount(" where  djstate=0");//待上传销售单记录数
            int Size = 20;//一次提取数量
            int pageIndex = 1;
            bool doing = true;
            string userName = "";
            DataTable dt = new DataTable();

            DataSet dsH = new DataSet();
            DataSet dsB = new DataSet();
            int count = 0;
            while (true)
            {
                dt = basicBLL.GetpfdHData(Size, pageIndex, ref count);

                if (dt != null && dt.Rows.Count > 0)
                {
                    dsH.Clear();
                    dsB.Clear();

                    dsH = new DataSet();
                    dsB = new DataSet();
                    dsH.Tables.Add(dt);//填装 表头部分
                    foreach (DataRow dr in dt.Rows)
                    {
                        xskdh xskdhModel = basicBLL.GetXskdhModel(dr["xsdh"].ToString());
                        userName = xskdhModel.djyh;
                        DataSet xskdbDs = basicBLL.GetxskdBDataDs("  where djlsh=" + dr["djlsh"].ToString());
                        dsB = FilledDs(dsB, xskdbDs.Tables[0]);
                    }

                    byte[] dtXskdh = GetDataSetSurrogateZipBYtes(dsH);

                    byte[] dtXskdb = GetDataSetSurrogateZipBYtes(dsB);


                    NewTransportObject transportobject = new NewTransportObject();
                    transportobject.StrXskdH = dtXskdh;
                    transportobject.DtXskdb = dtXskdb;
                    ArrayList parms = new ArrayList();
                    parms.Add(transportobject.StrXskdH);
                    parms.Add(transportobject.DtXskdb);
                    string reslut = DSWebService.InvokeWebService(url, "InsertYw_pfd", parms.ToArray());

                    if (int.Parse(reslut) > 0)
                    {
                        ArrayList parms2 = new ArrayList();
                        parms2.Add(userName);
                        reslut = DSWebService.InvokeWebService(url, "CheckXskdh", parms2.ToArray());

                        //修改本地数据库 xskdh状态
                        int s3 = basicBLL.UpdatexskdhStatus(dt);
                    }
                    else if (int.Parse(reslut) == -2)
                    {


                        LogHelper.m_CreateErrorLogTxt("UpLoadData", "0", "无法连接目标服务器请检查网络");
                        break;
                    }
                    else
                    {

                        LogHelper.m_CreateErrorLogTxt("UpLoadData", "0", "数据上传失败请联系管理员");
                        break;
                    }
                }
                else
                {
                    break;
                }
                pageIndex++;
            }
            if (count > 0)
                LogHelper.m_CreateErrorLogTxt("UpLoadData", "0", "上传完成 总共记录数目：" + count);


        }
        private DataSet FilledDs(DataSet ds, DataTable dt)
        {
            if (ds.Tables.Count > 0)
            {
                object[] obj = new object[dt.Columns.Count];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dt.Rows[i].ItemArray.CopyTo(obj, 0);
                    ds.Tables[0].Rows.Add(obj);
                }
            }
            else
            {
                DataTable ndt = dt.Copy();
                ds.Tables.Add(ndt);
            }

            return ds;

        }


        public static frmMain CreateInstance()
        {
            if (null == frmmain)
            {
                frmmain = new frmMain();
            }
            return frmmain;
        }
        void MiddleModule_eventSend(object sender, object receiver, object msg)
        {
            frmLogin frmLogin = sender as frmLogin;
            frmGathering gathering = sender as frmGathering;
            MemberManagement memberManagement = sender as MemberManagement;
            PrintAgain frmPrintAgain = sender as PrintAgain;
            frmGathering2 gathering2 = sender as frmGathering2;

            frmJyhf Jyhf = sender as frmJyhf;
            //数据来源登录页面
            if (null != frmLogin && frmmain.ToString() == receiver.ToString())
            {
                LoginInfo model = JsonConvert.DeserializeObject<LoginInfo>(msg.ToString());
                if (model != null)
                {
                    Syybh = model.Syybh;
                    Syymc = model.Syymc;
                    //默认 收银员 就是营业员
                    Yyybh = model.Yyybh;
                    Yyymc = model.Yyymc;

                    Dmbh = model.Dmbh;
                    Dmmc = model.Dmmc;
                }
            }

            //数据来源付款页面(会员付款)
            else if (null != gathering && frmmain.ToString() == receiver.ToString())
            {
                string[] aa = msg.ToString().Split(',');
                if (aa.Length == 8)
                {
                    Xj = aa[0];//现金
                    if (Xj == "")
                        Xj = "0";
                    Qk = aa[1];//欠款
                    Zl = aa[2];//找零

                    string strJfye = aa[3];
                    string strZhye = aa[4];

                    Bcjf = aa[5];//本次积分


                    SsJe = aa[6];//实收金额
                    Hykxf = aa[7];//会员卡消费

                    Ys = SsJe;//实际应收金额
                    //会员操作
                    if (strJfye != "" && strZhye != "")
                    {
                        Hyjf = decimal.Parse(strJfye);
                        Zhye = decimal.Parse(strZhye);
                    }

                    if (Zl != "")
                    {
                        //decimal dzl = decimal.Parse(Zl);


                        //decimal dxj = decimal.Parse(Xj);
                        //if (dzl > 0)
                        //{
                        //    Xj = (dxj - dzl).ToString();
                        //}
                    }
                }
            }
            //数据来源付款页面(普通消费)
            else if (null != gathering2 && frmmain.ToString() == receiver.ToString())
            {
                Sale model = JsonConvert.DeserializeObject<Sale>(msg.ToString());
                if (model != null)
                {
                    Zl = model.Zl;//找零
                    SsJe = model.SsJe;//实收金额
                    Ys = model.Ys; ;//实际应收金额
                    Bcjf = "0";
                    Xj = model.Xj;// aa[0];//现金
                    WX = model.Wx;// aa[0];//微信
                    YHK = model.Yhk;// aa[0];//银行卡
                    ZFB = model.Zfb;//支付宝
                }
            }

            //数据来源 交易恢复页面
            else if (null != Jyhf && frmmain.ToString() == receiver.ToString())
            {
                tshModel = new TempStorageH();
                tsBLL = new TempStorageBLL();
                tshModel = Common.Desrialize(tshModel, msg.ToString());
                Hybh = tshModel.Khbh;
                Hymc = tshModel.Khmc;
                Yyybh = tshModel.Yyybh;
                Yyymc = tshModel.Yyymc;

                this.ctxtKhmc.Text = Hymc;


                if (Hybh != null && Hybh != "")
                {
                    hykblhModel = hykblhBLL.GetModel(Hybh);
                    if (hykblhModel != null)
                    {
                        this.ctxtzk.Text = hykblhModel.hyzk.ToString();
                        this.ctxtKh.Text = Hybh;
                    }
                }

                Sl = tshModel.Sl.ToString();
                Ss = tshModel.Je.ToString();
                int tempDjh = tshModel.tempDjh;

                DataTable dt = tsBLL.GetTempStorageBData("tempDjlsh=" + tempDjh);


                spInfoData = null;
                spInfoData = CreateSpInfoDataData();


                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        DataRow ndr = spInfoData.NewRow();
                        ndr["hh"] = dr["hh"].ToString();
                        ndr["spbh"] = dr["spbh"].ToString();
                        ndr["spmc"] = dr["spmc"].ToString();
                        ndr["gg"] = dr["gg"].ToString();
                        ndr["dw"] = dr["dw"].ToString();

                        ndr["tmh"] = dr["tmh"].ToString();

                        ndr["sl"] = dr["sl"].ToString();
                        ndr["lsj"] = dr["dj"].ToString();

                        ndr["pfj"] = dr["pfj"].ToString();

                        ndr["ssje"] = dr["ssje"].ToString();
                        ndr["zke"] = dr["zke"].ToString();
                        ndr["zkl"] = dr["zkl"].ToString();
                        if (dr["bdz"].ToString() == "1")
                            ndr["bdz"] = true;
                        else
                            ndr["bdz"] = false;

                        ndr["gysmc"] = dr["gysmc"].ToString();
                        ndr["cjhh"] = dr["cjhh"].ToString();

                        ndr["jj"] = dr["jj"].ToString();

                        ndr["zhj"] = dr["zhj"].ToString();


                        spInfoData.Rows.Add(ndr);
                    }

                }

                this.dataGridView1.DataSource = spInfoData;

                //设置光标位置
                this.txtSpbh.Show();
                this.txtSpbh.Focus();


                #region 右下角数据显示
                this.ctxtZsl.Text = GetAllSl(spInfoData).ToString("0.00");

                this.ctxtZhk.Text = GetAllMoney(spInfoData).ToString("0.00");

                this.ctxtZqje.Text = GetWDZAllMoney(spInfoData).ToString("0.00");

                Sl = GetAllSl(spInfoData).ToString("0.00");
                Ys = GetWDZAllMoney(spInfoData).ToString("0.00");
                Ss = GetAllMoney(spInfoData).ToString("0.00");
                #endregion


            }
            //会员页面
            else if (null != memberManagement && frmmain.ToString() == receiver.ToString())
            {
                decimal czxj = 0;

                if (msg.ToString() != "")
                {
                    czxj = decimal.Parse(msg.ToString());
                }
                if (Zhye.ToString() != "")
                {
                    Zhye = Zhye + czxj;
                }
                else
                {
                    Zhye = czxj;//
                }


            }

            //数据来源 重打单
            else if (null != frmPrintAgain && frmmain.ToString() == receiver.ToString())
            {
                //销售单号
                string xsdh = "";

                string strHykje = "";

                string strs = msg.ToString();

                string[] ss = strs.Split('$');
                if (ss.Length == 2)
                {
                    if (cdyGuid != ss[1])
                    {
                        cdyGuid = ss[1];
                        string ss1 = ss[0];

                        string[] a1 = ss1.Split(',');
                        xsdh = a1[0];

                        Xsdh = xsdh;


                        xskdhBLL = new XskdhBLL();


                        xskdhModel = xskdhBLL.GetXskdhModel(xsdh);

                        if (xskdhModel != null)
                        {
                            Hybh = xskdhModel.khbh;
                            Hymc = xskdhModel.khmc;
                            Yyybh = xskdhModel.yyybh;
                            Yyymc = xskdhModel.yyymc;

                            Dmbh = xskdhModel.Bmbh;
                            Dmmc = xskdhModel.Bmmc;
                            strHykje = xskdhModel.hykxf.ToString();

                            //Xj = xskdhModel.xj.ToString();
                            //  Qk = xskdhModel.qk.ToString();

                            if (oldspInfoData == null)
                            {
                                oldspInfoData = CreateSpInfoDataData();
                            }
                            oldspInfoData = spInfoData;
                            spInfoData = null;
                            spInfoData = CreateSpInfoDataData();
                            //GetPfdBData
                            DataTable dt = xskdhBLL.GetPfdBData(xsdh);

                            if (dt != null && dt.Rows.Count > 0)
                            {
                                foreach (DataRow dr in dt.Rows)
                                {
                                    DataRow ndr = spInfoData.NewRow();
                                    ndr["hh"] = dr["hh"].ToString();
                                    ndr["spbh"] = dr["spbh"].ToString();

                                    ndr["tmh"] = dr["tmh"].ToString();

                                    ndr["spmc"] = dr["spmc"].ToString();
                                    ndr["gg"] = dr["gg"].ToString();
                                    ndr["dw"] = dr["dw"].ToString();
                                    ndr["sl"] = dr["sl"].ToString();
                                    ndr["lsj"] = dr["dj"].ToString();
                                    ndr["ssje"] = dr["je"].ToString();
                                    ndr["pfj"] = dr["pfj"].ToString();

                                    ndr["zkl"] = dr["hyzk"].ToString();

                                    // ndr["gysmc"] = dr["gysbh"].ToString();
                                    //  ndr["cjhh"] = dr["cjhh"].ToString();

                                    ndr["jj"] = dr["jj"].ToString();

                                    // ndr["zhj"] = dr["zhj"].ToString();
                                    spInfoData.Rows.Add(ndr);
                                }

                            }
                            //打印
                            posPrintDocument = new POSPrintDocument();
                            string zl = (xskdhModel.xj - xskdhModel.ssjehj).ToString();//应收 -实收(大于零 表示欠款 小于零  表示找零)
                            posPrintDocument.PrintXP(spInfoData, Hymc, Yyybh, Yyymc, Dmmc, strHykje, xskdhModel.xj.ToString(), xskdhModel.ssjehj.ToString(), zl);
                            //恢复值
                            spInfoData = oldspInfoData;
                            oldspInfoData = null;
                        }
                    }
                }
            }
        }
        /// <summary>
        ///   收款 (销售单)
        /// </summary>
        /// <returns></returns>
        private int SettleAccounts()
        {
            int success = 0;

            xskdhModel = new xskdh();

            xskdBModel = new xskdB();
            xskdhBLL = new XskdhBLL();




            List<string> sqlList = new List<string>();

            int djLsh = xskdhBLL.GetNewLsh();

            Xsdh = xskdhBLL.GetNewDJH(Dmbh);

            if (spInfoData != null && spInfoData.Rows.Count > 0)
            {
                xskdhModel.DjLsh = djLsh;
                xskdhModel.DjState = "0";//单据状态
                xskdhModel.xsdh = Xsdh;
                xskdhModel.yyybh = Yyybh;
                xskdhModel.yyymc = Yyymc;
                xskdhModel.Bmbh = Dmbh;
                xskdhModel.Bmmc = Dmmc;
                xskdhModel.khbh = Hybh;
                xskdhModel.khmc = Hymc;
                xskdhModel.hyk = Hybh;

                decimal ssje = 0;
                if (SsJe != null && SsJe != "")
                    ssje = decimal.Parse(SsJe);

                xskdhModel.ssjehj = ssje;


                decimal hykxf1 = 0;
                if (Hykxf != null && Hykxf != "")
                    hykxf1 = decimal.Parse(Hykxf);

                xskdhModel.hykxf = hykxf1;//会员卡消费 即实收金额



                //xskdhModel.kh.khlx = Hylx;
                xskdhModel.rq = DateTime.Now.ToString("yyyy-MM-dd");
                xskdhModel.sj = DateTime.Now.ToString();

                xskdhModel.djzt = "未审核";

                if (Xj == "" || Xj == null)
                    Xj = "0";

                if (Qk == "" || Qk == null)
                    Qk = "0";

                if (Zl == "" || Zl == null)
                    Zl = "0";

                xskdhModel.xj = decimal.Parse(Xj);//实际已经收取现金


                xskdhModel.jehj = GetWDZAllMoney(spInfoData);

                xskdhModel.slhj = GetAllSl(spInfoData);

                xskdhModel.yl = decimal.Parse(Xj);//银联
                // xskdhModel.wsk = decimal.Parse(Qk);

                // xskdhModel.zhjehj = GetAllMoney(spInfoData);
                // xskdhModel.yisk = GetAllMoney(spInfoData);

                xskdhModel.zrje = GetZKEAllMoney(spInfoData);

                xskdhModel.bqjf = decimal.Parse(Xj);

                xskdhModel.Zjf = Hyjf;
                xskdhModel.Hykye = Zhye.ToString();//会员卡余额
                //Zjxfjf
                xskdhModel.Zjxfjf = Bcjf;//本次消费积分

                xskdhModel.djyh = Zdr;
                xskdhModel.djsj = DateTime.Now;

                string xskdhStr = xskdhBLL.GetpfdHStringBuilder(xskdhModel);
                sqlList.Add(xskdhStr);
                string updateStr = xskdhBLL.GetStdjlshStringBuilder();
                sqlList.Add(updateStr);

                string updateStr2 = xskdhBLL.GetUpdateHykblHStringBuilder(xskdhModel);
                if (updateStr2 != "")
                    sqlList.Add(updateStr2);


                Type type = typeof(xskdB);
                Common2.PrintDataTypeByObj(spInfoData, type, "Get_Fault_Handle_Echarts_FaultNum");




                List<xskdB> ls = EntityMapper.ToList<xskdB>(spInfoData);
                int hh = 1;
                foreach (xskdB m1 in ls)
                {
                    xskdBModel.DjLsh = djLsh;
                    xskdBModel.hh = hh;
                    xskdBModel.tmh = xskdBModel.spbh;
                    xskdBModel.DjBth = hh;
                    xskdBModel.yyy = Yyybh;
                    spdahModel = spdaHBLL.GetModel(xskdBModel.spbh);
                    if (spdahModel != null)
                    {
                        xskdBModel.cm = spdahModel.cm;//尺码
                        xskdBModel.ysmc = spdahModel.ysmc;
                    }
                    xskdBModel.jj = 0;//进价
                    xskdBModel.zyhjg = xskdBModel.ssje;
                    xskdBModel.hyyh = 0;//会员优惠
                    xskdBModel.hxsdh = Xsdh;
                    string xskdbStr = xskdhBLL.GetpfdBStringBuilder(xskdBModel);
                    sqlList.Add(xskdbStr);
                    hh++;
                }



                //foreach (DataRow dr in spInfoData.Rows)
                //{
                //    decimal ddj = 0;
                //    decimal dsl = 0;
                //    decimal ddxj = 0;
                //    decimal dzkl = 0;

                //    xskdBModel.DjLsh = djLsh;
                //    xskdBModel.hh = hh;


                //    xskdBModel.spbh = dr["spbh"].ToString();
                //    xskdBModel.tmh = xskdBModel.spbh;
                //    xskdBModel.spmc = dr["spmc"].ToString();
                //    if (dr["lsj"].ToString() != "")
                //        ddj = decimal.Parse(dr["lsj"].ToString());
                //    xskdBModel.Dj = ddj;
                //    if (dr["sl"].ToString() != "")
                //        dsl = decimal.Parse(dr["sl"].ToString());
                //    xskdBModel.sl = dsl;
                //    if (dr["ssje"].ToString() != "")
                //        ddxj = decimal.Parse(dr["ssje"].ToString());
                //    xskdBModel.je = ddxj;
                //    if (dr["zkl"].ToString() != "")
                //        dzkl = decimal.Parse(dr["zkl"].ToString());
                //    xskdBModel.hyzk = dzkl;
                //    xskdBModel.DjBth = hh;
                //    xskdBModel.dw = dr["dw"].ToString();

                //    xskdBModel.yyy = Yyybh;

                //    //spdahModel;spdaHBLL

                //    spdahModel = spdaHBLL.GetModel(xskdBModel.spbh);
                //    if (spdahModel != null)
                //    {
                //        xskdBModel.cm = spdahModel.cm;//尺码
                //        xskdBModel.ysmc = spdahModel.ysmc;
                //    }

                //    xskdBModel.gg = dr["gg"].ToString();

                //    xskdBModel.sl = int.Parse(dr["sl"].ToString());



                //    xskdBModel.jj = 0;//进价
                //    xskdBModel.pfj = decimal.Parse(dr["pfj"].ToString()); //批发价
                //    xskdBModel.zyhjg = decimal.Parse(dr["ssje"].ToString());//折后金额
                //    xskdBModel.hyyh = 0;//会员优惠

                //    xskdBModel.hxsdh = Xsdh;

                //    string xskdbStr = xskdhBLL.GetpfdBStringBuilder(xskdBModel);
                //    sqlList.Add(xskdbStr);
                //    hh++;
                //}
                if (sqlList.Count > 0)
                {
                    success = xskdhBLL.SaveXskdData(sqlList);
                }
            }
            else
            {
                Message.MsgBoxWarn("产品不能空提交！");
            }
            return success;
        }

        #region 数据源相关操作
        /// <summary>
        /// 创建 splist数据源
        /// </summary>
        /// <param name="dt"></param>
        private void CreateListData(DataTable dt)
        {
            this.listSp.Items.Clear();
            //商品编号，商品名称，规格，单位
            foreach (DataRow dr in dt.Rows)
            {
                try
                {
                    string tmh = dr["tmh"].ToString();
                    string spbh = dr["spbh"].ToString();
                    string spmc = dr["spmc"].ToString();

                    //string spbh = dr["xlbh"].ToString().Replace("*", ":"); ;
                    //string spmc = dr["xlmc"].ToString();


                    string xlbh = dr["xlbh"].ToString();

                    string gg = dr["gg"].ToString();
                    string dw = dr["xdw"].ToString();
                    string spjm = dr["pyjm"].ToString();

                    string cm = dr["cm"].ToString();
                    string ysmc = dr["ysmc"].ToString();
                    //this.listSp.Items.Add(xlbh + " ，" + spmc + "，" + gg + "，" + dw + "，" + spjm);

                    this.listSp.Items.Add(tmh + " ，" + spbh + "，" + spmc + "，" + ysmc + "，" + cm);
                }
                catch { }

            }
            this.listSp.Visible = true;
            this.listSp.Show();
            this.listSp.Focus();
            this.listSp.SetSelected(0, true);

        }

        /// <summary>
        /// 创建数据集 spInfoData
        /// </summary>
        /// <returns></returns>
        private DataTable CreateSpInfoDataData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("hh", System.Type.GetType("System.Int32"));
            dt.Columns.Add("spbh");
            dt.Columns.Add("tmh");
            dt.Columns.Add("spmc");
            dt.Columns.Add("gg");
            dt.Columns.Add("dw");
            dt.Columns.Add("sl", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("lsj", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("pfj", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("ssje", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("zke");
            dt.Columns.Add("zkl", System.Type.GetType("System.Decimal"));
            dt.Columns.Add("zhj");//折后价
          
            dt.Columns.Add("bdz", System.Type.GetType("System.Boolean"));
            dt.Columns.Add("gysmc");
            dt.Columns.Add("cjhh");
            dt.Columns.Add("jj", System.Type.GetType("System.Decimal"));
            return dt;
        }
        /// <summary>
        /// 更新数据集 spInfoData   修改折扣率
        /// </summary>
        private void UpdateData(decimal newzkl)
        {

            if (spInfoData != null && spInfoData.Rows.Count > 0)
            {
                foreach (DataRow dr in spInfoData.Rows)
                {

                    dr.BeginEdit();
                    int sl = int.Parse(dr["sl"].ToString());//数量

                    bool bdz = bool.Parse(dr["bdz"].ToString());
                    if (!bdz)
                    {

                        decimal lsj = decimal.Parse(dr["lsj"].ToString());
                        decimal pfj = decimal.Parse(dr["pfj"].ToString());
                        decimal zhj = 0;
                        decimal xj = sl * lsj;//金额
                        decimal ssje = 0;//实收金额
                        #region  实收金额 根据用户选择而定
                        if (this.cbPfj.Checked)
                        {
                            ssje = sl * pfj * newzkl;//数量*批发价*折扣率
                            zhj = pfj * newzkl;//批发价*折扣率
                        }
                        else
                        {
                            ssje = sl * lsj * newzkl;//数量*零售价*折扣率
                            zhj = lsj * newzkl;//零售价*折扣率
                        }

                        #endregion


                        decimal zke = xj - ssje;//折扣额
                        dr["zkl"] = newzkl;
                        dr["ssje"] = ssje.ToString("0.00");
                        dr["zke"] = zke.ToString("0.00");
                        dr["zhj"] = zhj.ToString("0.00");
                    }

                    dr.EndEdit();
                }

                this.dataGridView1.DataSource = spInfoData;

                //设置光标位置
                this.txtSpbh.Show();
                this.txtSpbh.Focus();

                #region 右下角数据显示
                this.ctxtZsl.Text = GetAllSl(spInfoData).ToString("0.00");

                this.ctxtZhk.Text = GetAllMoney(spInfoData).ToString("0.00");

                this.ctxtZqje.Text = GetWDZAllMoney(spInfoData).ToString("0.00");

                Sl = GetAllSl(spInfoData).ToString("0.00");
                Ys = GetWDZAllMoney(spInfoData).ToString("0.00");
                Ss = GetAllMoney(spInfoData).ToString("0.00");
                #endregion
            }
        }

        /// <summary>
        /// 更新数据集 spInfoData F5 退货
        /// </summary>
        /// <param name="status"></param>
        private void UpdateData(bool status)
        {
            if (spInfoData != null && spInfoData.Rows.Count > 0)
            {

                foreach (DataRow dr in spInfoData.Rows)
                {

                    dr.BeginEdit();
                    int sl = int.Parse(dr["sl"].ToString());//数量
                    dr["sl"] = -sl;

                    decimal ssje = decimal.Parse(dr["ssje"].ToString());

                    dr["ssje"] = -ssje;

                    dr.EndEdit();
                }

                this.dataGridView1.DataSource = spInfoData;

                //设置光标位置
                this.txtSpbh.Show();
                this.txtSpbh.Focus();


                #region 右下角数据显示
                this.ctxtZsl.Text = GetAllSl(spInfoData).ToString("0.00");

                this.ctxtZhk.Text = GetAllMoney(spInfoData).ToString("0.00");

                this.ctxtZqje.Text = GetWDZAllMoney(spInfoData).ToString("0.00");

                Sl = GetAllSl(spInfoData).ToString("0.00");
                Ys = GetWDZAllMoney(spInfoData).ToString("0.00");
                Ss = GetAllMoney(spInfoData).ToString("0.00");
                #endregion

            }

        }

        /// <summary>
        /// 更新数据集 spInfoData 添加商品
        /// </summary>
        /// <param name="tmh"></param>
        /// <param name="cpbh"></param>
        private void UpdateData(string tmh, string spbh)
        {
            if (spInfoData == null)
            {
                spInfoData = CreateSpInfoDataData();
            }
            //判断该商品编号是否存在
            spbh = spbh.Replace("*", ":");
            string str = "spbh='" + spbh + "'";//  string str = "spbh='" + spbh + "'";

            DataRow[] drws = spInfoData.Select(str);
            decimal sl = 0;
            decimal lsj = 0;
            decimal pfj = 0;
            decimal jj = 0;//进价
            decimal zkl = 1;//默认为1
            decimal ssje = 0;//实收金额
            decimal xj = 0;//现金
            decimal zke = 0;//折扣额
            decimal zhj = 0;//折后价

            int hh = 0;
            if (this.cbXtsplj.Checked)
            {

                if (drws.Length > 0)
                {
                    spdaHBLL = new SpdaHBLL();

                    foreach (DataRow drw in drws)
                    {
                        drw.BeginEdit();

                        if (ScanNumber != 0)
                        {
                            if (drw["sl"].ToString() != "")
                                sl = decimal.Parse(drw["sl"].ToString());
                            sl++;
                        }

                        if (drw["lsj"].ToString() != "")
                            lsj = decimal.Parse(drw["lsj"].ToString().Trim());

                        if (drw["pfj"].ToString() != "")
                            pfj = decimal.Parse(drw["pfj"].ToString().Trim());


                        hh = int.Parse(drw["hh"].ToString().Trim());

                        drw["sl"] = sl;
                        if (this.cbPfj.Checked)
                        {
                            ssje = sl * pfj * zkl;
                            zhj = pfj * zkl;
                        }
                        else
                        {
                            ssje = sl * lsj * zkl;
                            zhj = lsj * zkl;
                        }
                        drw["lsj"] = lsj.ToString("0.00");
                        drw["pfj"] = pfj.ToString("0.00");
                        drw["ssje"] = ssje.ToString("0.00");
                        drw["zhj"] = zhj.ToString("0.00");
                        drw.EndEdit();
                        break;
                    }

                }
                else
                {
                    //只有一行
                    spdaHBLL = new SpdaHBLL();

                    tmh = tmh.Replace("*", ":");

                    //DataTable dt = spdaHBLL.GetList("   tmh='" + tmh.Trim() + "'");
                    DataTable dt = spdaHBLL.GetList("   spbh='" + spbh.Trim() + "'");
                    if (dt != null && dt.Rows.Count > 0)
                    {

                        DataRow ndr = spInfoData.NewRow();
                        ndr["hh"] = spInfoData.Rows.Count + 1;
                        ndr["spbh"] = dt.Rows[0]["spbh"].ToString().Trim().Replace("*", ":");
                        ndr["spmc"] = dt.Rows[0]["spmc"].ToString().Trim();
                        ndr["gg"] = dt.Rows[0]["gg"].ToString().Trim();
                        ndr["dw"] = dt.Rows[0]["xdw"].ToString().Trim();
                        ndr["tmh"] = tmh;
                        hh = int.Parse(ndr["hh"].ToString().Trim());
                        if (tmh != "")
                            sl = ScanNumber;

                        //零售价
                        if (dt.Rows[0]["jmlsj"].ToString().Trim() != "")
                        {
                            lsj = decimal.Parse(dt.Rows[0]["jmlsj"].ToString().Trim());
                        }
                        //批发价
                        if (dt.Rows[0]["pfj"].ToString().Trim() != "")
                        {
                            pfj = decimal.Parse(dt.Rows[0]["pfj"].ToString().Trim());
                        }
                        //进价
                        if (dt.Rows[0]["jj"].ToString().Trim() != "")
                        {
                            jj = decimal.Parse(dt.Rows[0]["jj"].ToString().Trim());
                        }

                        //判断采用批发价 还是零售价
                        if (this.cbPfj.Checked)
                        {
                            ssje = sl * pfj * zkl;
                            zhj = pfj * zkl;
                        }
                        else
                        {
                            ssje = sl * lsj * zkl;
                            zhj = lsj * zkl;
                        }
                        ndr["lsj"] = lsj.ToString("0.00");
                        ndr["pfj"] = pfj.ToString("0.00");

                        xj = sl * lsj;//现金
                        zke = xj - ssje;//折扣额


                        ndr["jj"] = jj.ToString("0.00");
                        ndr["sl"] = sl.ToString("0.");

                        ndr["ssje"] = ssje.ToString("0.00");
                        ndr["zke"] = zke.ToString("0.00");
                        ndr["zkl"] = zkl.ToString("0.00");
                        ndr["zhj"] = zhj.ToString("0.00");

                        ndr["bdz"] = false;

                        ndr["gysmc"] = dt.Rows[0]["gysmc"].ToString().Trim();

                        ndr["cjhh"] = "";// dt.Rows[0]["cjhh"].ToString().Trim();


                        spInfoData.Rows.Add(ndr);
                    }
                }

            }
            else
            {
                //只有一行
                spdaHBLL = new SpdaHBLL();
                tmh = tmh.Replace("*", ":");
                //  DataTable dt = spdaHBLL.GetList("   tmh='" + tmh.Trim() + "'");

                DataTable dt = spdaHBLL.GetList("   spbh='" + spbh.Trim() + "'");
                if (dt != null && dt.Rows.Count > 0)
                {

                    DataRow ndr = spInfoData.NewRow();
                    ndr["hh"] = spInfoData.Rows.Count + 1;
                    ndr["spbh"] = dt.Rows[0]["xlbh"].ToString().Trim().Replace("*", ":");//dt.Rows[0]["spbh"].ToString().Trim().Replace("*", ":");
                    ndr["spmc"] = dt.Rows[0]["xlmc"].ToString().Trim();//dt.Rows[0]["spmc"].ToString().Trim();
                    ndr["gg"] = dt.Rows[0]["gg"].ToString().Trim();
                    ndr["dw"] = dt.Rows[0]["xdw"].ToString().Trim();
                    ndr["tmh"] = tmh;

                    hh = int.Parse(ndr["hh"].ToString().Trim());
                    if (tmh != "")
                        sl = ScanNumber;


                    if (dt.Rows[0]["jmlsj"].ToString().Trim() != "")
                    {
                        lsj = decimal.Parse(dt.Rows[0]["jmlsj"].ToString().Trim());
                    }
                    //批发价
                    if (dt.Rows[0]["pfj"].ToString().Trim() != "")
                    {
                        pfj = decimal.Parse(dt.Rows[0]["pfj"].ToString().Trim());
                    }

                    //判断采用批发价 还是零售价
                    if (this.cbPfj.Checked)
                    {
                        ssje = sl * pfj * zkl;
                        zhj = pfj * zkl;
                    }
                    else
                    {
                        ssje = sl * lsj * zkl;
                        zhj = lsj * zkl;
                    }
                    xj = sl * lsj;
                    zke = xj - ssje;
                    ndr["pfj"] = pfj.ToString("0.00");
                    ndr["lsj"] = lsj.ToString("0.00");
                    ndr["sl"] = sl.ToString("0.");

                    ndr["ssje"] = ssje.ToString("0.00");
                    ndr["zke"] = zke.ToString("0.00");
                    ndr["zkl"] = zkl.ToString("0.00");
                    ndr["zhj"] = zhj.ToString("0.00");
                    ndr["bdz"] = false;

                    ndr["gysmc"] = dt.Rows[0]["gysmc"].ToString().Trim();

                    ndr["cjhh"] = dt.Rows[0]["cjhh"].ToString().Trim();
                    spInfoData.Rows.Add(ndr);
                }
            }


            this.listSp.Visible = false;
            this.listSp.Items.Clear();
            CreateMaterialImg(tmh, 1);
            this.dataGridView1.DataSource = spInfoData;


            this.dataGridView1.FirstDisplayedScrollingRowIndex = this.dataGridView1.Rows.Count - 1;

            if (hh > 0)
                hh--;
            this.dataGridView1.CurrentCell = this.dataGridView1.Rows[hh].Cells[4];


            //设置光标位置
            this.txtSpbh.Show();
            this.txtSpbh.Focus();

            #region 右下角数据显示
            this.ctxtZsl.Text = GetAllSl(spInfoData).ToString("0.00");

            this.ctxtZhk.Text = GetAllMoney(spInfoData).ToString("0.00");

            this.ctxtZqje.Text = GetWDZAllMoney(spInfoData).ToString("0.00");

            Sl = GetAllSl(spInfoData).ToString("0.00");
            Ys = GetWDZAllMoney(spInfoData).ToString("0.00");
            Ss = GetAllMoney(spInfoData).ToString("0.00");
            #endregion



        }


        /// <summary>
        /// 更新数据集 spInfoData  删除
        /// </summary>
        /// <param name="tmh"></param>
        /// <param name="cpbh"></param>
        private void UpdateData(string cpbh, int hh)
        {
            if (spInfoData.Rows[hh]["spbh"].ToString() == cpbh)
            {
                spInfoData.Rows.RemoveAt(hh);

                int rh = 0;
                foreach (DataRow drw in spInfoData.Rows)
                {
                    rh++;
                    drw.BeginEdit();
                    drw["hh"] = rh;
                    drw.EndEdit();
                }
            }
            this.cImg.Image = null;//图片指控
            this.dataGridView1.DataSource = spInfoData;

            //设置光标位置
            this.txtSpbh.Show();
            this.txtSpbh.Focus();

            #region 右下角数据显示
            this.ctxtZsl.Text = GetAllSl(spInfoData).ToString("0.00");

            this.ctxtZhk.Text = GetAllMoney(spInfoData).ToString("0.00");

            this.ctxtZqje.Text = GetWDZAllMoney(spInfoData).ToString("0.00");

            Sl = GetAllSl(spInfoData).ToString("0.00");
            Ys = GetWDZAllMoney(spInfoData).ToString("0.00");
            Ss = GetAllMoney(spInfoData).ToString("0.00");
            #endregion

        }


        /// <summary>
        /// 更新数据集 spInfoData
        /// </summary>
        /// <param name="cpbh">商品编号</param>
        /// <param name="hh">行号</param>
        /// <param name="type">类型 0 单价， 1 数量 2.折扣率 单项</param>
        private void UpdateData(string cpbh, int hh, int type, decimal value)
        {
            if (spInfoData.Rows[hh]["spbh"].ToString() == cpbh)
            {
                decimal zkl = decimal.Parse(spInfoData.Rows[hh]["zkl"].ToString());

                decimal lsj = 0;

                if (spInfoData.Rows[hh]["lsj"].ToString() != "")
                    lsj = decimal.Parse(spInfoData.Rows[hh]["lsj"].ToString().Trim());

                //修改单价
                if (type == 0)
                {
                    spInfoData.Rows[hh].BeginEdit();
                    int sl = int.Parse(spInfoData.Rows[hh]["sl"].ToString());

                    decimal dj = value;//单价

                    //判断采用批发价 还是零售价
                    if (this.cbPfj.Checked)
                    {
                        spInfoData.Rows[hh]["pfj"] = dj;

                    }
                    else
                    {
                        spInfoData.Rows[hh]["lsj"] = dj;
                        lsj = dj;//如果修改的是零售价
                    }




                    decimal xj = sl * lsj;//金额
                    decimal ssje = sl * dj * zkl;//实收金额
                    decimal zhj = dj * zkl;//单价(零售价、批发价)*折扣率
                    decimal zke = xj - ssje;//折扣额


                    spInfoData.Rows[hh]["ssje"] = ssje.ToString("0.00");
                    spInfoData.Rows[hh]["zke"] = zke.ToString("0.00");
                    spInfoData.Rows[hh]["zhj"] = zhj.ToString("0.00");
                    spInfoData.Rows[hh].EndEdit();
                }
                //修改数量
                else if (type == 1)
                {
                    spInfoData.Rows[hh].BeginEdit();

                    //    int oldsl = int.Parse(spInfoData.Rows[hh]["sl"].ToString());//原来数量

                    int newsl = Convert.ToInt16(value);//新增数量
                    int sl = newsl;
                    decimal dj = 0;//注意这里dj 变量并不表示 零售价而 根据 用户选择 this.cbPfj.Checked 而定
                    //判断采用批发价 还是零售价
                    if (this.cbPfj.Checked)
                    {
                        dj = decimal.Parse(spInfoData.Rows[hh]["pfj"].ToString());
                    }
                    else
                    {
                        dj = decimal.Parse(spInfoData.Rows[hh]["lsj"].ToString());
                    }

                    decimal xj = sl * lsj;//金额
                    decimal ssje = sl * dj * zkl;//实收金额
                    decimal zhj = dj * zkl;//单价(零售价、批发价)*折扣率

                    decimal zke = xj - ssje;//折扣额

                    spInfoData.Rows[hh]["sl"] = sl;

                    spInfoData.Rows[hh]["ssje"] = ssje.ToString("0.00");
                    spInfoData.Rows[hh]["zke"] = zke.ToString("0.00");
                    spInfoData.Rows[hh]["zhj"] = zhj.ToString("0.00");

                    spInfoData.Rows[hh].EndEdit();
                }
                else
                {
                    spInfoData.Rows[hh].BeginEdit();
                    int sl = int.Parse(spInfoData.Rows[hh]["sl"].ToString());//原来数量
                    decimal newzkl = value;//新折扣率
                    // decimal lsj = decimal.Parse(spInfoData.Rows[hh]["lsj"].ToString());


                    decimal dj = 0;//注意这里dj 变量并不表示零售价  而是 根据 用户选择 this.cbPfj.Checked 而定
                    //判断采用批发价 还是零售价
                    if (this.cbPfj.Checked)
                    {
                        dj = decimal.Parse(spInfoData.Rows[hh]["pfj"].ToString());
                    }
                    else
                    {
                        dj = decimal.Parse(spInfoData.Rows[hh]["lsj"].ToString());
                    }


                    decimal xj = sl * lsj;//金额
                    decimal ssje = sl * dj * newzkl;//实收金额
                    decimal zhj = dj * newzkl;//单价(零售价、批发价)*折扣率


                    decimal zke = xj - ssje;//折扣额
                    spInfoData.Rows[hh]["zkl"] = newzkl;
                    spInfoData.Rows[hh]["ssje"] = ssje.ToString("0.00"); ;
                    spInfoData.Rows[hh]["zke"] = zke.ToString("0.00"); ;
                    spInfoData.Rows[hh]["zhj"] = zhj.ToString("0.00"); ;

                    spInfoData.Rows[hh].EndEdit();
                }

            }
            this.dataGridView1.DataSource = spInfoData;

            //设置光标位置
            this.txtSpbh.Show();
            this.txtSpbh.Focus();

            #region 右下角数据显示
            this.ctxtZsl.Text = GetAllSl(spInfoData).ToString("0.00");

            this.ctxtZhk.Text = GetAllMoney(spInfoData).ToString("0.00");

            this.ctxtZqje.Text = GetWDZAllMoney(spInfoData).ToString("0.00");

            Sl = GetAllSl(spInfoData).ToString("0.00");
            Ys = GetWDZAllMoney(spInfoData).ToString("0.00");
            Ss = GetAllMoney(spInfoData).ToString("0.00");
            #endregion



        }
        #endregion

        #region 销售运算

        /// <summary>
        /// 实收金额合计
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        private decimal GetAllMoney(DataTable dt)
        {
            decimal allMoney = 0;
            decimal xj = 0;
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["ssje"].ToString() != "")
                    xj = decimal.Parse(dr["ssje"].ToString());
                allMoney += xj;
            }

            //if (allMoney < 0)
            //    allMoney = 0;
            return allMoney;
        }

        /// <summary>
        /// 金额合计(未打折时)
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        private decimal GetWDZAllMoney(DataTable dt)
        {
            decimal allMoney = 0;
            decimal xj = 0;
            decimal je = 0;
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["lsj"].ToString() != "")
                    xj = decimal.Parse(dr["lsj"].ToString());
                je = xj * int.Parse(dr["sl"].ToString());
                allMoney += je;
            }
            //if (allMoney < 0)
            //    allMoney = 0;
            return allMoney;
        }

        /// <summary>
        /// 成本价金额合计
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        private decimal GetCBJAllMoney(DataTable dt)
        {
            decimal allMoney = 0;
            decimal xj = 0;
            decimal je = 0;

            foreach (DataRow dr in dt.Rows)
            {
                if (dr["jj"].ToString() != "")
                    xj = decimal.Parse(dr["jj"].ToString());
                je = xj * int.Parse(dr["sl"].ToString());
                allMoney += je;
            }
            //if (allMoney < 0)
            //    allMoney = 0;
            return allMoney;

        }


        /// <summary>
        /// 折扣额 合计
        /// </summary>
        /// <returns></returns>
        private decimal GetZKEAllMoney(DataTable dt)
        {
            decimal allMoney = 0;
            decimal xj = 0;
            decimal je = 0;
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["zke"].ToString() != "")
                    xj = decimal.Parse(dr["zke"].ToString());
                je = xj * int.Parse(dr["sl"].ToString());
                allMoney += je;
            }

            return allMoney;
        }

        /// <summary>
        /// 数量合计
        /// </summary>
        /// <returns></returns>
        private decimal GetAllSl(DataTable dt)
        {
            decimal allSl = 0;
            decimal sl = 0;
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["sl"].ToString() != "")
                    sl = decimal.Parse(dr["sl"].ToString());
                allSl += sl;
            }
            return allSl;
        }

        #endregion

        /// <summary>
        /// 样式
        /// </summary>
        /// <param name="dt"></param>
        private void SetdataGridStyle()
        {

            try
            {

                // this.cDataGridView1.RowHeadersVisible = false;
                this.dataGridView1.RowHeadersVisible = true;

                //序号
                DataGridViewTextBoxColumn dc = new DataGridViewTextBoxColumn();
                dc.HeaderText = "序号";
                dc.DataPropertyName = "hh";
                dc.HeaderCell.Style.Font = new Font("宋体", 13, FontStyle.Bold);
                dc.HeaderCell.Style.ForeColor = Color.Blue;
                dc.DefaultCellStyle.Font = new Font("宋体", 13, FontStyle.Bold);
                dataGridView1.Columns.Add(dc);
                this.dataGridView1.Columns[0].Width = 52;
                this.dataGridView1.Columns[0].ReadOnly = true;
                this.dataGridView1.Columns[0].Visible = false;
                //商品条码
                dc = new DataGridViewTextBoxColumn();
                dc.HeaderText = "商品条码";
                dc.DataPropertyName = "tmh";
                dc.HeaderCell.Style.Font = new Font("宋体", 13, FontStyle.Bold);
                dc.HeaderCell.Style.ForeColor = Color.Blue;
                dc.DefaultCellStyle.Font = new Font("宋体", 13, FontStyle.Bold);
                dataGridView1.Columns.Add(dc);
                this.dataGridView1.Columns[1].Width = 90;
                this.dataGridView1.Columns[1].ReadOnly = true;
                this.dataGridView1.Columns[1].Visible = false;

                //商品名称
                dc = new DataGridViewTextBoxColumn();
                dc.HeaderText = "商品名称";
                dc.DataPropertyName = "spmc";
                dc.HeaderCell.Style.Font = new Font("宋体", 13, FontStyle.Bold);
                dc.HeaderCell.Style.ForeColor = Color.Blue;
                dc.DefaultCellStyle.Font = new Font("宋体", 13, FontStyle.Bold);
                dataGridView1.Columns.Add(dc);
                this.dataGridView1.Columns[2].Width = 250;
                this.dataGridView1.Columns[2].ReadOnly = true;


                //规格
                dc = new DataGridViewTextBoxColumn();
                dc.HeaderText = "规格";
                dc.DataPropertyName = "gg";
                dc.HeaderCell.Style.Font = new Font("宋体", 13, FontStyle.Bold);
                dc.HeaderCell.Style.ForeColor = Color.Blue;
                dc.DefaultCellStyle.Font = new Font("宋体", 13, FontStyle.Bold);
                dataGridView1.Columns.Add(dc);
                this.dataGridView1.Columns[3].Width = 52;
                this.dataGridView1.Columns[3].Visible = false;
                //单位
                dc = new DataGridViewTextBoxColumn();
                dc.HeaderText = "单位";
                dc.DataPropertyName = "dw";
                dc.HeaderCell.Style.Font = new Font("宋体", 13, FontStyle.Bold);
                dc.HeaderCell.Style.ForeColor = Color.Blue;
                dc.DefaultCellStyle.Font = new Font("宋体", 13, FontStyle.Bold);
                dataGridView1.Columns.Add(dc);
                this.dataGridView1.Columns[4].Width = 52;
                this.dataGridView1.Columns[4].ReadOnly = true;

                //数量
                dc = new DataGridViewTextBoxColumn();
                dc.HeaderText = "数量";
                dc.DataPropertyName = "sl";
                dc.HeaderCell.Style.Font = new Font("宋体", 13, FontStyle.Bold);
                dc.HeaderCell.Style.ForeColor = Color.Blue;
                dc.DefaultCellStyle.Font = new Font("宋体", 13, FontStyle.Bold);
                dataGridView1.Columns.Add(dc);
                this.dataGridView1.Columns[5].Width = 52;


                //单价
                dc = new DataGridViewTextBoxColumn();
                dc.HeaderText = "单价";
                dc.DataPropertyName = "lsj";
                dc.HeaderCell.Style.Font = new Font("宋体", 13, FontStyle.Bold);
                dc.HeaderCell.Style.ForeColor = Color.Blue;
                dc.DefaultCellStyle.Font = new Font("宋体", 13, FontStyle.Bold);
                dataGridView1.Columns.Add(dc);
                this.dataGridView1.Columns[6].Width = 72;
                this.dataGridView1.Columns[6].ReadOnly = true;

                //实收金额
                dc = new DataGridViewTextBoxColumn();
                dc.HeaderText = "实收金额";
                dc.DataPropertyName = "ssje";
                dc.HeaderCell.Style.Font = new Font("宋体", 13, FontStyle.Bold);
                dc.HeaderCell.Style.ForeColor = Color.Blue;
                dc.DefaultCellStyle.Font = new Font("宋体", 13, FontStyle.Bold);
                dataGridView1.Columns.Add(dc);
                this.dataGridView1.Columns[7].Width = 100;


                //折扣额
                dc = new DataGridViewTextBoxColumn();
                dc.HeaderText = "折扣额";
                dc.DataPropertyName = "zke";
                dc.HeaderCell.Style.Font = new Font("宋体", 13, FontStyle.Bold);
                dc.HeaderCell.Style.ForeColor = Color.Blue;
                dc.DefaultCellStyle.Font = new Font("宋体", 13, FontStyle.Bold);
                dataGridView1.Columns.Add(dc);
                this.dataGridView1.Columns[8].Width = 80;
                this.dataGridView1.Columns[8].ReadOnly = true;


                //折扣率
                dc = new DataGridViewTextBoxColumn();
                dc.HeaderText = "折扣率";
                dc.DataPropertyName = "zkl";
                dc.HeaderCell.Style.Font = new Font("宋体", 13, FontStyle.Bold);
                dc.HeaderCell.Style.ForeColor = Color.Blue;
                dc.DefaultCellStyle.Font = new Font("宋体", 13, FontStyle.Bold);
                dataGridView1.Columns.Add(dc);
                this.dataGridView1.Columns[9].Width = 80;

                //营业员
                dc = new DataGridViewTextBoxColumn();
                dc.HeaderText = "营业员";
                dc.DataPropertyName = "yyy";
                dc.HeaderCell.Style.Font = new Font("宋体", 15, FontStyle.Bold);
                dc.HeaderCell.Style.ForeColor = Color.Blue;
                dc.DefaultCellStyle.Font = new Font("宋体", 13, FontStyle.Bold);
                dataGridView1.Columns.Add(dc);
                this.dataGridView1.Columns[10].Width = 100;

                //不打折
                //    dc = new DataGridViewTextBoxColumn();
                DataGridViewCheckBoxColumn newColumn = new DataGridViewCheckBoxColumn();
                newColumn.HeaderText = "不打折";
                newColumn.DataPropertyName = "bdz";
                newColumn.HeaderCell.Style.Font = new Font("宋体", 13, FontStyle.Bold);
                newColumn.HeaderCell.Style.ForeColor = Color.Blue;
                newColumn.DefaultCellStyle.Font = new Font("宋体", 13, FontStyle.Bold);
                dataGridView1.Columns.Add(newColumn);
                this.dataGridView1.Columns[11].Width = 100;

                //厂家
                dc = new DataGridViewTextBoxColumn();
                dc.HeaderText = "厂家";
                dc.DataPropertyName = "gysmc";
                dc.HeaderCell.Style.Font = new Font("宋体", 13, FontStyle.Bold);
                dc.HeaderCell.Style.ForeColor = Color.Blue;
                dc.DefaultCellStyle.Font = new Font("宋体", 13, FontStyle.Bold);
                dataGridView1.Columns.Add(dc);
                this.dataGridView1.Columns[12].Width = 100;
                this.dataGridView1.Columns[12].Visible = false;
                //厂家货号
                dc = new DataGridViewTextBoxColumn();
                dc.HeaderText = "厂家货号";
                dc.DataPropertyName = "cjhh";
                dc.HeaderCell.Style.Font = new Font("宋体", 13, FontStyle.Bold);
                dc.HeaderCell.Style.ForeColor = Color.Blue;
                dc.DefaultCellStyle.Font = new Font("宋体", 13, FontStyle.Bold);
                dataGridView1.Columns.Add(dc);
                this.dataGridView1.Columns[13].Width = 110;

                //商品编号
                dc = new DataGridViewTextBoxColumn();
                dc.HeaderText = "商品编号";
                dc.DataPropertyName = "spbh";
                dc.HeaderCell.Style.Font = new Font("宋体", 13, FontStyle.Bold);
                dc.HeaderCell.Style.ForeColor = Color.Blue;
                dc.DefaultCellStyle.Font = new Font("宋体", 13, FontStyle.Bold);
                dataGridView1.Columns.Add(dc);
                this.dataGridView1.Columns[14].Width = 110;
                this.dataGridView1.Columns[14].Visible = false;

                //批发价
                dc = new DataGridViewTextBoxColumn();
                dc.HeaderText = "批发价";
                dc.DataPropertyName = "pfj";
                dc.HeaderCell.Style.Font = new Font("宋体", 13, FontStyle.Bold);
                dc.HeaderCell.Style.ForeColor = Color.Blue;
                dc.DefaultCellStyle.Font = new Font("宋体", 13, FontStyle.Bold);
                dataGridView1.Columns.Add(dc);
                this.dataGridView1.Columns[15].Width = 110;
                this.dataGridView1.Columns[15].Visible = false;

                //折后价
                dc = new DataGridViewTextBoxColumn();
                dc.HeaderText = "折后价";
                dc.DataPropertyName = "zhj";
                dc.HeaderCell.Style.Font = new Font("宋体", 13, FontStyle.Bold);
                dc.HeaderCell.Style.ForeColor = Color.Blue;
                dc.DefaultCellStyle.Font = new Font("宋体", 13, FontStyle.Bold);
                dataGridView1.Columns.Add(dc);
                this.dataGridView1.Columns[16].Width = 110;
                this.dataGridView1.Columns[16].Visible = false;

                //进价
                dc = new DataGridViewTextBoxColumn();
                dc.HeaderText = "进价";
                dc.DataPropertyName = "jj";
                dc.HeaderCell.Style.Font = new Font("宋体", 13, FontStyle.Bold);
                dc.HeaderCell.Style.ForeColor = Color.Blue;
                dc.DefaultCellStyle.Font = new Font("宋体", 13, FontStyle.Bold);
                dataGridView1.Columns.Add(dc);
                this.dataGridView1.Columns[17].Width = 110;
                this.dataGridView1.Columns[17].Visible = false;
            }
            catch { }
        }

        #region 热键设置

        private void frmMain_Activated(object sender, EventArgs e)
        {
            HotKey.RegisterHotKey(Handle, 100, HotKey.KeyModifiers.None, Keys.F1);//帮助
            HotKey.RegisterHotKey(Handle, 101, HotKey.KeyModifiers.None, Keys.F2);//客户
            HotKey.RegisterHotKey(Handle, 102, HotKey.KeyModifiers.None, Keys.F3);//单价
            HotKey.RegisterHotKey(Handle, 103, HotKey.KeyModifiers.None, Keys.F4);//数量
            HotKey.RegisterHotKey(Handle, 104, HotKey.KeyModifiers.None, Keys.F5);//退货
            HotKey.RegisterHotKey(Handle, 105, HotKey.KeyModifiers.None, Keys.F6);//折扣
            HotKey.RegisterHotKey(Handle, 106, HotKey.KeyModifiers.None, Keys.F7);//销售日报表
            HotKey.RegisterHotKey(Handle, 107, HotKey.KeyModifiers.None, Keys.F8);//付费
            HotKey.RegisterHotKey(Handle, 108, HotKey.KeyModifiers.None, Keys.F9);//打印开关
            HotKey.RegisterHotKey(Handle, 109, HotKey.KeyModifiers.None, Keys.F11);//重打单
            HotKey.RegisterHotKey(Handle, 110, HotKey.KeyModifiers.None, Keys.Escape);//Esc
            HotKey.RegisterHotKey(Handle, 111, HotKey.KeyModifiers.None, Keys.Delete);//删除
            HotKey.RegisterHotKey(Handle, 112, HotKey.KeyModifiers.Ctrl, Keys.D);//弹出收银箱
            // HotKey.RegisterHotKey(Handle, 113, HotKey.KeyModifiers.Alt, Keys.E);//光标进入文本框
            HotKey.RegisterHotKey(Handle, 114, HotKey.KeyModifiers.Ctrl, Keys.Tab);//光标进入文本框

            //设置光标位置
            this.txtSpbh.Focus();
        }

        private void EnterTxtSpbh()
        {
            this.Show();
            this.txtSpbh.Focus();
        }
        /// <summary>
        /// 打开银箱
        /// </summary>
        private void OpenCashbox1()
        {
            XmlDocument doc = new XmlDocument();
            string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\config.xml";
            doc.Load(path);    //加载Xml文件  
            XmlElement rootElem = doc.DocumentElement;   //获取根节点  

            XmlNodeList personNodes = rootElem.GetElementsByTagName("PrintSet");
            string portName = "";


            foreach (XmlNode node in personNodes)
            {
                XmlElement xe = (XmlElement)node;//将子节点类型转换为XmlElement类型
                portName = xe.GetAttribute("PortName");
            }




            POSPrinter prn = new POSPrinter(portName);
            string str = prn.PrintLine("");
            if (str != "")
                MessageBox.Show(str);
        }
        /// <summary>
        /// 删除选择行
        /// </summary>
        private void DeleteSelectRow()
        {
            //删除当前选中行
            try
            {
                string str = this.dataGridView1.SelectedCells[0].Value.ToString();
                int index = this.dataGridView1.CurrentCell.RowIndex;
                string spbh = this.dataGridView1.Rows[index].Cells[2].Value.ToString();
                UpdateData(spbh, index);
            }
            catch { }
        }

        /// <summary>
        /// 修改选择行
        /// </summary>
        /// <param name="type">0 修改 单价，1  修改数量,2 折扣 单项,3 折扣 全部</param>
        private void UpdataSelectRow(int type)
        {
            if (type != 3)
            {
                try
                {
                    string str = this.dataGridView1.SelectedCells[0].Value.ToString();
                    int index = this.dataGridView1.CurrentCell.RowIndex;
                    string spbh = this.dataGridView1.Rows[index].Cells[2].Value.ToString();
                    decimal value = 0;
                    try
                    {
                        value = decimal.Parse(this.txtSpbh.Text.Trim());
                        UpdateData(spbh, index, type, value);
                    }
                    catch { }

                }
                catch { }
            }
            else
            {
                try
                {
                    decimal value = decimal.Parse(this.txtSpbh.Text.Trim());
                    Zdzk = value;/*整单折让*/
                    UpdateData(value);
                }
                catch { }
            }



        }

        /// <summary>
        /// 退出 Esc键
        /// </summary>
        private void EscListSp()
        {
            try
            {
                if (this.listSp.Visible)
                {
                    this.listSp.Items.Clear();
                    this.listSp.Visible = false;

                    this.Show();
                    this.txtSpbh.Focus();
                }
                else
                {
                    if (Message.AskQuestion("确定要退出系统", "退出提醒"))
                    {
                        Application.Exit();
                    }
                }

            }
            catch { }

        }



        private void frmMain_Leave(object sender, EventArgs e)
        {
            HotKey.UnregisterHotKey(Handle, 100);
            HotKey.UnregisterHotKey(Handle, 101);
            HotKey.UnregisterHotKey(Handle, 102);
            HotKey.UnregisterHotKey(Handle, 103);
            HotKey.UnregisterHotKey(Handle, 104);
            HotKey.UnregisterHotKey(Handle, 105);
            HotKey.UnregisterHotKey(Handle, 106);
            HotKey.UnregisterHotKey(Handle, 107);
            HotKey.UnregisterHotKey(Handle, 108);
            HotKey.UnregisterHotKey(Handle, 109);
            HotKey.UnregisterHotKey(Handle, 110);
            HotKey.UnregisterHotKey(Handle, 111);
            HotKey.UnregisterHotKey(Handle, 112);
            // HotKey.UnregisterHotKey(Handle, 113);
            HotKey.UnregisterHotKey(Handle, 114);
        }

        /// 重载FromA中的WndProc函数
        /// 监视Windows消息
        /// 重载WndProc方法，用于实现热键响应
        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            const int WM_HOTKEY = 0x0312;
            //按快捷键
            switch (m.Msg)
            {
                case WM_HOTKEY:
                    switch (m.WParam.ToInt32())
                    {
                        case 100:
                            Help1();
                            break;
                        case 101:
                            ShowMerberManage();
                            break;
                        case 102:
                            SetDj();
                            break;
                        case 103:
                            SetSl();
                            break;
                        case 104:
                            SetTh();
                            break;
                        case 105:
                            SetZk();
                            break;
                        case 106:
                            ShowDateReport();
                            break;
                        case 107:
                            SetFf();
                            break;
                        case 108:
                            SetPrint1();
                            break;
                        case 109:
                            PrintAgains();
                            break;
                        case 110:
                            SetEsc();
                            break;
                        case 111:
                            DeleteSelectRow();
                            break;
                        case 112:
                            OpenCashbox1();
                            break;
                        case 114:
                            SetCursorSite();
                            break;
                    }
                    break;
            }

            base.WndProc(ref m);
        }


        #endregion
        private void frmMain_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;

            this.notifyIcon1.ContextMenuStrip = contextMenuStrip1;
        }

        private void cPanel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void gbtnF2_Click(object sender, EventArgs e)
        {
            ShowMerberManage();
        }

        private void gbtnF5_Click(object sender, EventArgs e)
        {
            SetTh();
        }

        private void gbtnF1_Click(object sender, EventArgs e)
        {
            Help1();
        }



        /// <summary>
        /// 敲回车、扫条码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtSpbh_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {

                spdaHBLL = new SpdaHBLL();
                string Pyjm = this.txtSpbh.Text.Trim();
                bool isYyy = false;
                //输入不为空
                if (Pyjm != "")
                {

                    if (Pyjm.Length == 3 || Pyjm.Length == 4 || Pyjm.Length == 5 || Pyjm.Length == 2 || Pyjm.Length == 1)
                    {

                        BasicBLL basicBLL = new BasicBLL();
                        string yyymc = basicBLL.GetDataByFiled("ymmc", "yyydaH", " where ygbh='" + Pyjm + "'");

                        if (yyymc != "")
                        {
                            isYyy = true;
                            Yyybh = Pyjm;
                            Yyymc = yyymc;
                            this.ctxtYyy.Text = Yyymc;
                        }

                    }
                    if (!isYyy)
                    {
                        //string strWhere = "   pyjm like '" + Pyjm + "%' or spbh  like '" + Pyjm + "%'  or tmh    like '" + Pyjm + "%'";

                        string strWhere = "   pyjm like '" + Pyjm + "%' or xlbh  like '" + Pyjm + "%'  or tmh    like '" + Pyjm + "%'";
                        string sql = "";
                        DataTable spDt = spdaHBLL.GetList(strWhere, ref sql);

                        if (spDt != null && spDt.Rows.Count > 0)
                        {
                            //  CreateListData(spDt);



                            //当商品记录确定只有一条
                            if (spDt.Rows.Count == 1)
                            {
                                string tmh = spDt.Rows[0]["tmh"].ToString();
                                string cpbh = spDt.Rows[0]["spbh"].ToString(); // spDt.Rows[0]["xlbh"].ToString(); 
                                UpdateData(tmh, cpbh);


                                try
                                {
                                    string hyzk = ctxtzk.Text.Trim();

                                    if (hyzk != "")
                                    {
                                        decimal value = decimal.Parse(hykblhModel.hyzk.ToString()) / 10;
                                        Zdzk = value;/*整单折让*/
                                        UpdateData(value);
                                    }
                                }
                                catch { }

                            }
                            else
                            {
                                CreateListData(spDt);
                            }

                        }
                        else
                        {
                            Message.MsgBoxWarn("该产品不存在！");
                        }
                    }
                    this.txtSpbh.Text = "";

                }
                //输入为空
                else
                {
                    //  listbox 不为空
                    if (this.listSp.Items.Count > 0)
                    {
                        string values = this.listSp.SelectedItem.ToString();
                        if (values != "")
                        {
                            string[] aa = values.Split('，');
                            if (aa.Length > 0)
                            {
                                string tmh = aa[0].Trim();
                                string cpbh = aa[1].Trim();
                                UpdateData(tmh, cpbh);
                            }
                        }
                    }
                    else
                    {
                        SetFf();
                    }
                }
            }
        }

        private void PrintToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }



        private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void OpenCashbox_Click(object sender, EventArgs e)
        {

        }

        private void cbTyzk_Click(object sender, EventArgs e)
        {
            // common.CheckGroup(this.cbTyzk, this.panel2.Controls);
        }

        private void cbDxzk_Click(object sender, EventArgs e)
        {
            // Common.CheckGroup(this.cbDxzk, this.panel2.Controls);
        }

        private void cbPfj_Click(object sender, EventArgs e)
        {
            //Common.CheckGroup(this.cbPfj, this.panel1.Controls);
        }

        private void cbJmlsj_Click(object sender, EventArgs e)
        {
            //   Common.CheckGroup(this.cbJmlsj, this.panel1.Controls);
        }


        #region 功能方法

        private void Help1()
        {
            Help help = Help.CreateInstance();
            if (!help.Visible)
                help.Show();
        }



        private void SetDj()
        {
            UpdataSelectRow(0);
            this.txtSpbh.Text = "";
        }
        private void SetSl()
        {
            UpdataSelectRow(1);
            this.txtSpbh.Text = "";
        }

        private void SetTh()
        {
            if (Th)
            {
                Th = false;
                UpdateData(false);
                for (int i = 0; i < this.dataGridView1.Rows.Count - 1; i++)
                {
                    this.dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.White;
                }
            }
            else
            {
                Th = true;
                UpdateData(Th);
                for (int i = 0; i < this.dataGridView1.Rows.Count - 1; i++)
                {
                    this.dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                }

            }
        }



        private void SetZk()
        {
            try
            {
                decimal value = decimal.Parse(this.txtSpbh.Text.Trim());
                if (value > 1)
                {
                    //按照MessageBox的标题，找到MessageBox的窗口  
                    IntPtr ptr = CloseWindow.FindWindow(null, "MessageBox1");

                    int key = CloseWindow.FindMessageBox("ZKTX");
                    if (key == 0)
                    {
                        MessageBox.Show("折扣不能大于1！", "ZKTX");//折扣提醒
                    }

                }
                else
                {
                    if (this.cbDxzk.Checked)
                    {
                        UpdataSelectRow(2);
                    }
                    if (this.cbTyzk.Checked)
                    {
                        UpdataSelectRow(3);
                    }
                }
            }
            catch { }

            this.txtSpbh.Text = "";
        }

        private void SetPrint1()
        {
            if (PrintState == 1)
            {
                PrintState = 0;
                this.labPrint.Text = "关";
            }
            else
            {
                PrintState = 1;
                this.labPrint.Text = "开";
            }
        }

        private void SetEsc()
        {
            int key = CloseWindow.FindMessageBox("清屏提醒");
            if (key == 0)
            {
                //清屏提醒
                // MessageBox.Show("清屏后数据不在恢复 确定清屏？！", "清屏提醒", MessageBoxButtons.OKCancel);

                if (Message.AskQuestion("清屏后数据不在恢复 确定清屏？", "清屏提醒"))
                {
                    spInfoData = CreateSpInfoDataData();
                    this.cImg.Image = null;
                    this.dataGridView1.DataSource = spInfoData;
                }
            }


        }

        private void SetFf()
        {

            if (spInfoData != null && spInfoData.Rows.Count > 0)
            {
                decimal allM = GetAllMoney(spInfoData);
                //应收款$会员余额$会员卡号$会员卡余额
                if (Hykh != null && Hykh != "")
                {
                    string msg = allM.ToString() + "$" + Hyjf.ToString() + "$" + Hykh + "$" + Zhye.ToString();
                    frmGathering gathering = frmGathering.CreateInstance();
                    MiddleModule.SendMessage(this, gathering, msg);
                    gathering.ShowDialog();
                }
                else
                {
                    SaleStatus model = new SaleStatus();
                    model.YS = allM;
                    string msg = JsonConvert.SerializeObject(model);

                    frmGathering2 gathering2 = frmGathering2.CreateInstance();
                    MiddleModule.SendMessage(this, gathering2, msg);
                    gathering2.ShowDialog();
                }
                if (Xj != null && Xj != "")
                {
                    int success = SettleAccounts();

                    if (success > 0)
                    {
                        //设置 值
                        //Xj = aa[0];//现金
                        //Qk = aa[1];//欠款
                        //Zl = aa[2];//找零
                        //Ss
                        this.ctxtYs.Text = Ss;
                        this.ctxtSs.Text = Xj;

                        if (Zl != null && Zl != "")
                        {
                            decimal zl = decimal.Parse(Zl);

                            if (zl != 0)
                            {
                                this.ctxtZl.Text = zl.ToString("0.00");
                            }
                            else
                            {
                                if (Qk != null && Qk != "")
                                {
                                    decimal qk = decimal.Parse(Qk);

                                    this.ctxtZl.Text = qk.ToString("0.00");
                                }
                            }
                        }
                        posPrintDocument = new POSPrintDocument();

                        decimal hykje = 0;
                        string strHykje = "";
                        if (Hybh != null && Hybh != "")
                        {
                            if (Xj != null && Xj != "" && Ys != null && Ys != "")
                            {

                                try
                                {
                                    decimal zl = 0;
                                    decimal qk = 0;
                                    try
                                    {
                                        zl = decimal.Parse(Zl);
                                    }
                                    catch { }
                                    try
                                    {
                                        qk = decimal.Parse(Qk);
                                    }
                                    catch { }


                                    hykje = decimal.Parse(Ys) + zl - decimal.Parse(Xj) - qk;

                                    strHykje = Hykxf;
                                }
                                catch { }
                            }
                        }


                        //    string yyyName = this.ctxtYyy.Text.Trim();

                        if (PrintState != 0)
                        {

                            posPrintDocument.PrintXP(spInfoData, Hymc, Syybh, Yyymc, Dmmc, strHykje, Xj, Ys, Zl);
                        }


                        //初始化页面属性

                        //Yyybh = "";
                        //Yyymc = "";
                        Sl = "";
                        Ss = "";
                        Xj = "";
                        Qk = "";
                        Zl = "";

                        Hybh = "";
                        Hymc = "";
                        Zhye = 0;
                        Hyjf = 0;
                        Hykh = "";


                        this.ctxtKhmc.Text = "";
                        this.ctxtzk.Text = "";
                        this.ctxtHykye.Text = "";
                        this.txtJfye.Text = "";
                        this.ctxtKh.Text = "";
                        spInfoData = CreateSpInfoDataData();
                        this.dataGridView1.DataSource = spInfoData;

                    }
                    else
                        Message.MsgBoxInfo("操作失败");
                }
            }
        }


        private void PrintAgains()
        {
            PrintAgain printAgain = PrintAgain.CreateInstance();
            if (!printAgain.Visible)
                printAgain.ShowDialog();
        }
        private void ShowDateReport()
        {
            DateReport dateReport = DateReport.CreateInstance();
            if (!dateReport.Visible)
                dateReport.ShowDialog();
        }


        private void ShowHyxx()
        {

        }


        private void ShowMerberManage()
        {
            MemberManagement memberManagement = MemberManagement.CreateInstance();

            MemberInfo model = new MemberInfo();

            model.Hykh = Hykh;
            model.Dmbh = Dmbh;
            model.Dmmc = Dmmc;
            model.Yyymc = Yyymc;

            //会员卡号、店面编号、店面名称、营业员
            string msg = JsonConvert.SerializeObject(model);// Hykh + "," + Dmbh + "," + Dmmc + "," + Yyymc;
            MiddleModule.SendMessage(this, memberManagement, msg);

            if (!memberManagement.Visible)
                memberManagement.ShowDialog();
        }
        #endregion

        private void listSp_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                //情况一  listbox 不为空
                if (this.listSp.Items.Count > 0)
                {
                    string values = this.listSp.SelectedItem.ToString();
                    if (values != "")
                    {
                        string[] aa = values.Split('，');
                        if (aa.Length > 0)
                        {
                            string tmh = aa[0].Trim();
                            string cpbh = aa[1].Trim();
                            UpdateData(tmh, cpbh);

                            try
                            {
                                string hyzk = ctxtzk.Text.Trim();

                                if (hyzk != "")
                                {
                                    decimal value = decimal.Parse(hykblhModel.hyzk.ToString()) / 10;
                                    Zdzk = value;/*整单折让*/
                                    UpdateData(value);
                                }
                            }
                            catch { }

                        }
                    }

                }
            }
        }

        private void gbtnCz_Click(object sender, EventArgs e)
        {
            frmHycz hycz = frmHycz.CreateInstance();

            if (Hykh != null && Hykh != "")
            {

                //会员卡号、店面编号、店面名称、营业员
                string msg = Hykh + "," + Dmbh + "," + Dmmc + "," + Yyymc;
                MiddleModule.SendMessage(this, hycz, msg);

                if (!hycz.Visible)
                    hycz.ShowDialog();

                SetHykValue(0);

            }
            else
            {
                Message.MsgBoxWarn("会员卡号不能为空");
            }
        }



        private void gbtnF8_Click(object sender, EventArgs e)
        {
            SetFf();
        }


        public void CreateMaterialImg(string spbh, int type)
        {
            Common commonBLL = new Common();
            string tmh = spbh.ToString();
            byte[] bytes = commonBLL.ReadImgStream(tmh, type);

            if (bytes != null)
            {
                if (bytes.Length > 0)
                {
                    try
                    {
                        MemoryStream ms = new MemoryStream(bytes);//创建流

                        //生产图片 保存
                        System.Drawing.Image img = System.Drawing.Bitmap.FromStream(ms);
                        Bitmap bmp = new Bitmap(img);
                        this.cImg.Image = bmp;
                        this.cImg.SizeMode = PictureBoxSizeMode.Zoom;
                    }
                    catch { this.cImg.Image = null; }

                }
                else
                {
                    this.cImg.Image = null;
                }
            }


        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {

                int rowIndex = e.RowIndex;
                var _spbh = dataGridView1.Rows[rowIndex].Cells[2].Value;

                if (_spbh != null)
                {
                    string spbh = _spbh.ToString();
                    CreateMaterialImg(spbh, 0);
                }



                //设置光标位置
                this.txtSpbh.Show();
                this.txtSpbh.Focus();

            }
            catch { }
        }

        private void ctxtKh_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                SetHykValue(1);
            }
        }
        private void SetHykValue(int type)
        {
            string hykh = this.ctxtKh.Text.Trim();

            hykblhBLL = new HykblHBLL(url);
            hykblhModel = hykblhBLL.GetModel(hykh, Dmbh);
            if (hykblhModel != null)
            {
                Hybh = hykblhModel.kh;//会员编号(这里采用会员卡号)
                Hymc = hykblhModel.khmc;//会员名称
                if (hykblhModel.jfye.ToString() != "")
                {
                    Hyjf = decimal.Parse(hykblhModel.jfye.ToString());//积分余额
                }


                Hykh = hykblhModel.kh;//会员卡号
                if (hykblhModel.hykye != null && hykblhModel.hykye.ToString() != "")
                    Zhye = decimal.Parse(hykblhModel.hykye.ToString());//会员卡余额
                else
                    Zhye = 0;

                this.ctxtKhmc.Text = Hymc;
                this.ctxtzk.Text = hykblhModel.hyzk.ToString();
                this.ctxtHykye.Text = Zhye.ToString();
                this.txtJfye.Text = Hyjf.ToString();

                decimal value = 1;
                try
                {
                    if (type == 1)
                    {
                        value = decimal.Parse(hykblhModel.hyzk.ToString()) / 10;
                        Zdzk = value;/*整单折让*/
                        UpdateData(value);
                    }
                }
                catch { }
            }
            else
            {
                if (type == 1)
                {
                    Hybh = "";
                    Hymc = "";
                    Hyjf = 0;
                    Zhye = 0;
                    this.ctxtKhmc.Text = "";
                    this.ctxtzk.Text = "";
                    this.ctxtHykye.Text = "";
                    this.txtJfye.Text = "";

                }


            }
        }

        private void gbtnF6_Click(object sender, EventArgs e)
        {
            SetZk();
        }

        private void gbtnJyzt_Click(object sender, EventArgs e)
        {

        }

        private void gbtnJyhf_Click(object sender, EventArgs e)
        {
        
        }

        private void frmMain_Deactivate(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                HotKey.UnregisterHotKey(Handle, 100);
                HotKey.UnregisterHotKey(Handle, 101);
                HotKey.UnregisterHotKey(Handle, 102);
                HotKey.UnregisterHotKey(Handle, 103);
                HotKey.UnregisterHotKey(Handle, 104);
                HotKey.UnregisterHotKey(Handle, 105);
                HotKey.UnregisterHotKey(Handle, 106);
                HotKey.UnregisterHotKey(Handle, 107);
                HotKey.UnregisterHotKey(Handle, 108);
                HotKey.UnregisterHotKey(Handle, 109);
                HotKey.UnregisterHotKey(Handle, 110);
                HotKey.UnregisterHotKey(Handle, 111);
                HotKey.UnregisterHotKey(Handle, 112);
                HotKey.UnregisterHotKey(Handle, 114);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            string time = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
            labInfo.Text = "操作员：" + Yyymc + "  店面：" + Dmmc + "  时间：" + time;
        }

        private void gbtnF12_Click(object sender, EventArgs e)
        {
            PrintAgains();
        }

        private void gbtnF4_Click(object sender, EventArgs e)
        {
            SetSl();
        }

        private void gbtnF3_Click(object sender, EventArgs e)
        {
            SetDj();
        }

        private void gbtnF9_Click(object sender, EventArgs e)
        {
            SetPrint1();
        }

        private void gbtnF7_Click(object sender, EventArgs e)
        {
            ShowDateReport();
        }



        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            notifyIcon1.Visible = false;
            this.Show();
            WindowState = FormWindowState.Maximized;
            this.Focus();
        }

        private void frmMain_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)    //最小化到系统托盘
            {
                this.notifyIcon1.Visible = true;  //显示托盘图标
                this.Hide();    //隐藏窗口
            }
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            //Application.Exit();
            //注意判断关闭事件Reason来源于窗体按钮，否则用菜单退出时无法退出!
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;    //取消"关闭窗口"事件
                this.WindowState = FormWindowState.Minimized;    //使关闭时窗口向右下角缩小的效果
                this.notifyIcon1.Visible = true;
                this.Hide();
                return;
            }
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            notifyIcon1.Visible = false;
            this.Show();
            this.WindowState = FormWindowState.Maximized;
            this.Activate();
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            this.notifyIcon1.Visible = false;

            this.Close();

            this.Dispose();

            Application.Exit();
        }


        private void SetCursorSite()
        {
            //CursorSite

            this.Show();

            if (CursorSite == 0)
            {
                this.ctxtKh.Focus();
                CursorSite = 1;
            }
            else
            {
                this.txtSpbh.Focus();
                CursorSite = 0;
            }

        }

        #region 序列化 反序列化




        /// 序列化DataTable
        /// </summary>
        /// <param name="pDt">包含数据的DataTable</param>
        /// <returns>序列化的DataTable</returns>
        public static string SerializeDataTableXml(DataTable pDt, string pdtname)
        {
            // 序列化DataTable
            pDt.TableName = pdtname;
            StringBuilder sb = new StringBuilder();
            XmlWriter writer = XmlWriter.Create(sb);
            XmlSerializer serializer = new XmlSerializer(typeof(DataTable));
            serializer.Serialize(writer, pDt);
            writer.Close();
            return sb.ToString();
        }




        ///序列化转化为二进制数组的DataSet
        public byte[] GetDataSetSurrogateZipBYtes(DataSet DS)
        {
            DataSetSurrogate dss = new DataSetSurrogate(DS);
            ///二进制方式方式序列化

            BinaryFormatter ser = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            ser.Serialize(ms, dss);
            byte[] buffer = ms.ToArray();
            ///调用压缩方法

            byte[] Zipbuffer = Compress(buffer);
            return Zipbuffer;
        }
        ///压缩二进制文件
        private byte[] Compress(byte[] data)
        {
            MemoryStream ms = new MemoryStream();
            Stream zipStream = null;
            zipStream = new GZipStream(ms, CompressionMode.Compress, true);

            ///从指定的字节数组中将压缩的字节写入基础流

            zipStream.Write(data, 0, data.Length);
            zipStream.Close();
            ms.Position = 0;
            byte[] Compressed_Data = new byte[ms.Length];
            ms.Read(Compressed_Data, 0, int.Parse(ms.Length.ToString()));
            return Compressed_Data;
        }



        ///将压缩后的二进制数组解压

        public static byte[] Decompress(byte[] data)
        {      ///data参数为压缩后的二进制数组
            try
            {
                MemoryStream ms = new MemoryStream(data);
                Stream zipStream = null;
                zipStream = new GZipStream(ms, CompressionMode.Decompress);
                byte[] dc_data = null;
                dc_data = EtractBytesFormStream(zipStream, data.Length);
                return dc_data;

                ///返回解压后的二进制数组
            }
            catch
            {
                return null;
            }
        }


        public static byte[] EtractBytesFormStream(Stream zipStream, int dataBlock)
        {
            try
            {
                byte[] data = null;
                int totalBytesRead = 0;
                while (true)
                {
                    Array.Resize(ref data, totalBytesRead + dataBlock + 1);
                    int bytesRead = zipStream.Read(data, totalBytesRead, dataBlock);
                    if (bytesRead == 0)
                    {
                        break;
                    }
                    totalBytesRead += bytesRead;
                }
                Array.Resize(ref data, totalBytesRead);
                return data;
            }
            catch
            {
                return null;
            }
        }


        /// 将二进制文件反序列化后转化为DataSet

        public DataSet GetDatasetFromByte(byte[] ZipByte)
        {
            byte[] buffer = Decompress(ZipByte);
            BinaryFormatter ser = new BinaryFormatter();
            DataSetSurrogate dss;
            dss = (DataSetSurrogate)ser.Deserialize(new MemoryStream(buffer));
            DataSet DS = dss.ConvertToDataSet();
            return DS;

        }


        /// <summary>
        /// 反序列化DataTable
        /// </summary>
        /// <param name="pXml">序列化的DataTable</param>
        /// <returns>DataTable</returns>
        public static DataTable DeserializeDataTable(string pXml)
        {

            StringReader strReader = new StringReader(pXml);
            XmlReader xmlReader = XmlReader.Create(strReader);
            XmlSerializer serializer = new XmlSerializer(typeof(DataTable));

            DataTable dt = serializer.Deserialize(xmlReader) as DataTable;
            return dt;
        }


        #endregion



        private void PrinterSetToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmSetPrint setprint = frmSetPrint.CreateInstance();
            setprint.Show();
        }

        private void OpenLinksToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenCashbox1();
        }

        private void CardManagementToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MemberList memberList = MemberList.CreateInstance();
            memberList.Dmbh = Dmbh;
            memberList.Dmmc = Dmmc;
            memberList.Yyy = Yyymc;

            memberList.Show();
        }

        private void TemplateSetToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PosSet posSet = new PosSet();

            posSet.ShowDialog();
        }

        private void SystemSettingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmConfig systemSet = frmConfig.CreateInstance();
            systemSet.ShowDialog();
        }

        private void ManualSyncToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SynchTool synchTool = new SynchTool();
            //Dmbh
            synchTool.Dmbh = Dmbh;
            synchTool.Show();
        }

        private void RevocationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UpdateData(1);
            Sl = "";
            Ss = "";
            Xj = "";
            Qk = "";
            Zl = "";

            Hybh = "";
            Hymc = "";
            Zhye = 0;
            Hyjf = 0;
            Hykh = "";


            this.ctxtKhmc.Text = "";
            this.ctxtzk.Text = "";
            this.ctxtHykye.Text = "";
            this.txtJfye.Text = "";
            this.ctxtKh.Text = "";
        }

        private void ExitToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void TradingHaltToolStripMenuItem_Click(object sender, EventArgs e)
        {

            tshModel = new TempStorageH();
            tsbModel = new TempStorageB();
            tsBLL = new TempStorageBLL();
            List<string> sqlList = new List<string>();

            if (spInfoData != null && spInfoData.Rows.Count > 0)
            {
                int djlsh = tsBLL.GetNewLsh();

                tshModel.tempDjh = djlsh;
                tshModel.Khbh = Hybh;
                tshModel.Khmc = Hymc;
                tshModel.Yyybh = Yyybh;
                tshModel.Yyymc = Yyymc;
                float sl = float.Parse(Sl.ToString());
                tshModel.Sl = int.Parse(sl.ToString());//数量
                tshModel.Je = decimal.Parse(Ss);//实收金额
                tshModel.TempTime = DateTime.Now.ToString();

                string xskdhStr = tsBLL.GetTempStorageHStr(tshModel);
                sqlList.Add(xskdhStr);

                foreach (DataRow dr in spInfoData.Rows)
                {
                    tsbModel.hh = int.Parse(dr["hh"].ToString());
                    tsbModel.spbh = dr["spbh"].ToString();

                    tsbModel.tmh = dr["tmh"].ToString();
                    tsbModel.spmc = dr["spmc"].ToString();
                    tsbModel.gg = dr["gg"].ToString();
                    tsbModel.dw = dr["dw"].ToString();
                    tsbModel.tempDjlsh = djlsh;
                    if (dr["sl"].ToString() != null && dr["sl"].ToString() != "")
                    {
                        tsbModel.sl = int.Parse(dr["sl"].ToString());
                    }
                    if (dr["lsj"].ToString() != null && dr["lsj"].ToString() != "")
                    {
                        tsbModel.dj = decimal.Parse(dr["lsj"].ToString());
                    }

                    if (dr["pfj"].ToString() != null && dr["pfj"].ToString() != "")
                    {
                        tsbModel.pfj = decimal.Parse(dr["pfj"].ToString());
                    }

                    if (dr["ssje"].ToString() != null && dr["ssje"].ToString() != "")
                    {
                        tsbModel.ssje = decimal.Parse(dr["ssje"].ToString());
                    }
                    if (dr["zke"].ToString() != null && dr["zke"].ToString() != "")
                    {
                        tsbModel.zke = decimal.Parse(dr["zke"].ToString());
                    }
                    if (dr["zkl"].ToString() != null && dr["zkl"].ToString() != "")
                    {
                        tsbModel.zkl = decimal.Parse(dr["zkl"].ToString());
                    }
                    if (dr["zhj"].ToString() != null && dr["zhj"].ToString() != "")
                    {
                        tsbModel.zhj = decimal.Parse(dr["zhj"].ToString());
                    }


                    tsbModel.bdz = 0;
                    if (dr["bdz"].ToString() != null && dr["bdz"].ToString() != "")
                    {
                        if (bool.Parse(dr["bdz"].ToString()))
                            tsbModel.bdz = 1;

                    }
                    tsbModel.cjhh = dr["cjhh"].ToString();


                    if (dr["jj"].ToString() != null && dr["jj"].ToString() != "")
                    {
                        tsbModel.jj = decimal.Parse(dr["jj"].ToString());
                    }
                    else
                        tsbModel.jj = 0;

                    tsbModel.gysmc = dr["gysmc"].ToString();
                    string xskdbStr = tsBLL.GetTempStorageStringBuilder(tsbModel);
                    sqlList.Add(xskdbStr);

                }

                int ss = tsBLL.ExecuteSqlTranBuilder(sqlList);

                if (ss > 0)
                {
                    //初始化页面属性
                    Hybh = "";
                    Hymc = "";
                    Yyybh = "";
                    Yyymc = "";
                    Sl = "";
                    Ss = "";

                    this.ctxtKh.Text = "";
                    this.ctxtKhmc.Text = "";
                    this.ctxtzk.Text = "";
                    //this.cTextBox4.Text = "";
                    //this.txtZjf.Text = "";
                    this.cImg.Image = null;


                    Zhye = 0;
                    Hyjf = 0;
                    Hykh = "";




                    spInfoData = CreateSpInfoDataData();
                    this.dataGridView1.DataSource = spInfoData;
                    Message.MsgBoxInfo("交易已经暂停");
                }
                else
                {
                    Message.MsgBoxWarn("操作失败！请联系管理员");
                }
            }
        }

        private void TradingResumedToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmJyhf jyhf = frmJyhf.CreateInstance();
            if (!jyhf.Visible)
                jyhf.ShowDialog();
        }
    }
}
